package chujisuanfa;

public class CountAndSay {
    public static String countAndSay(int n){
        if(n==1) return "1";
        StringBuilder result = new StringBuilder(); //结果
        int count  = 1; //当前数字出现的次数

        String prev= countAndSay(n-1);
        char num = prev.charAt(0);
        for(int i =1; i<prev.length();++i){
            while(i<prev.length()&&prev.charAt(i)==num) {
                count++;
                i++;
            }
            result.append(count);
            result.append(num);
            if (i>=prev.length()) return result.toString();
            count=1;
            num = prev.charAt(i);
        }
        result.append(count);
        result.append(num);
        return result.toString();
    }

    public static void main(String[] args) {
        System.out.println(countAndSay(5));
    }
}
